﻿using Opc.Ua;
using Opc.Ua.Client;
using OpcUaHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OPCUADemo
{
    public class Program
    {
        static async Task Main(string[] args)
        {
            try
            {
                OpcUaClient opcUaClient = new OpcUaClient();
                // 匿名登录，无需用户名和密码
                //opcUaClient.UserIdentity = new UserIdentity(new AnonymousIdentityToken());
                // 用户名密码的方式
                opcUaClient.UserIdentity = new UserIdentity("Tuling", "Tuling");

                var serverAddress = "opc.tcp://127.0.0.1:49320/Kepware.KEPServerEX.V6";
                await opcUaClient.ConnectServer(serverAddress);

                Console.WriteLine("OPC UA 连接成功");

                // 读取数据:ns=2是固定写法
                var address1 = "ns=2;s=ModbusRtu.ModbusRTUOne.温度数据";
                var addressValue1 = await opcUaClient.ReadNodeAsync<ushort>(address1);
                Console.WriteLine("温度数据:" + addressValue1);

                // 批量读取
                var address2 = "ns=2;s=ModbusRtu.ModbusRTUOne.压力数据";
                var tags = new List<string>();
                tags.Add(address1);
                tags.Add(address2);
                var valueList = await opcUaClient.ReadNodesAsync<ushort>(tags.ToArray());

                foreach (var item in valueList)
                {
                    Console.WriteLine("批量读取的值：" + item);
                }

                // 如果批量读取的类型不一样
                //var nodeIds = new List<NodeId>();
                //nodeIds.Add(new NodeId(address1));
                //nodeIds.Add(new NodeId(address2));
                //var dataValues = await opcUaClient.ReadNodesAsync(nodeIds.ToArray());
                //foreach (var item in dataValues)
                //{
                //    Console.WriteLine("批量读取的值：" + item);
                //}

                // 写入单个值
                var random = new Random();
                var randomValue = random.Next(1, 10);
                await opcUaClient.WriteNodeAsync(address1, (ushort)randomValue);

                // 批量写入
                var isWriteSuccess = opcUaClient.WriteNodes(
                    new string[] { address1, address2 },
                    new object[] { (ushort)22, (ushort)23 }
                );

                if (isWriteSuccess)
                {
                    Console.WriteLine("批量写入成功!");
                }

                // 订阅模式
                opcUaClient.AddSubscription("A", address1, SubCallBack);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.ReadKey();
        }

        private static void SubCallBack(string key, MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs args)
        {
            if (key == "A")
            {
                var notification = args.NotificationValue as MonitoredItemNotification;

                if (notification != null)
                {
                    Console.WriteLine("来自订阅的温度数据:" + notification.Value.WrappedValue.Value.ToString());
                }
            }
        }
    }
}
